import { axios, api } from '@/Axios/index.js';
import _ from 'lodash';
import { setNumSort } from './fun.js';

export default {
  namespaced: true,//platform_app
  state: {
    ARRSORT: ['杭州顶点财经二分', '顶点二分', '顶点财经二分'],
    workBenchList: [],
    expandPid: [],
    activePid: '',
    activeApp: {},
  },
  mutations: {
    initPlatformApp: function (state, list) {
      list.map((workBenches) => {
        return {
          numSort: setNumSort(workBenches.companyName, state.ARRSORT),
          name: workBenches.companyName,// 公司名称
          id: workBenches.eSCompanyId,// 企业微信id
          // unread: workBenches.noReadingCount,//后面setworkBenchList有要求偏差的统计（非置顶的不参与）
          list: workBenches.agentList.sort((a, b) => {
            return b.dSortId - a.dSortId;//	置顶排序
          }).map((agent) => {
            return {
              pid: workBenches.eSCompanyId,
              name: agent.name,// 应用名称
              id: agent.agentid,// 应用ID
              icon: agent.squarelogourl,// 企业应用方形头像
              isTop: agent.isTop,// 是否置顶
              isHidden: !agent.isHide,// 是否隐藏,后端没有修改的意图,只好前端适配了
              unread: agent.noReadingCount// 未读数
            }
          })
        }
      }).sort((a, b) => {
        return b.numSort - a.numSort
      }).forEach((workBenches) => {
        state.workBenchList.push(workBenches)
      })
    },

    折叠平台: function (ctx, { pid }) {
      if (pid) {
        let idx = ctx.expandPid.indexOf(pid)
        if (idx > -1) {
          ctx.expandPid.splice(idx, 1)
        } else {
          ctx.expandPid.push(pid);
        }
      } else {
        ctx.expandPid.splice(0, ctx.expandPid.length)
      }
      return
    },
    激活平台: function (ctx, { pid }) {
      ctx.activePid = pid;
    },
    激活应用: function (ctx, { app }) {
      ctx.activePid = app.pid;
      ctx.activeApp = Object.assign({}, app);
    },
    updataUnread: function (ctx, { pid, aid, nUnread }) {//nUnread可正可负
      let p = ctx.workBenchList.find((v) => { return v.id == pid })
      let app = _.get(p, 'list', []).find((v) => { return v.id = aid; })
      p.unread += nUnread;
      app.unread += nUnread;
    }
  },
  getters: {
    getActivePlatform: function (ctx) {
      return function (useKey) {
        if (_.isString(useKey)) {
          let obj = {};
          obj[useKey] = _.get(ctx, 'activePid', '')
          return obj;
        }
        let p = ctx.workBenchList.find((v) => {
          return v.id == ctx.activePid
        })
        return p;
      }
    },
    getActiveApp: function (ctx) {
      return function (useKey) {
        if (_.isString(useKey)) {
          let obj = {};
          obj[useKey] = _.get(ctx, 'activeApp.id', '')
          return obj;
        }
        let p = ctx.workBenchList.find((v) => {
          return v.id == ctx.activeApp.pid
        })
        let app = _.get(p, 'list', []).find((v) => {
          return v.id = ctx.activeApp.id;
        })
        return app;
      }

    },
    isSameApp: function (ctx) {
      return function (app) {
        let apid = _.get(ctx, 'activeApp.pid', false);
        if (!apid) { return false }
        let aaid = _.get(ctx, 'activeApp.id', false);
        if (!aaid) { return false }
        return (apid == app.pid) && (aaid == app.id);
      }
    },

  },
  actions: {
    init: function (ctx, { ceid: ceid }) {
      return api.get_App_Cus({
        companyEmployeeId: ceid,
        state: 0,
      }).then((res) => {
        let arr = _.get(res.data, 'workBenches.workBenchList', [])
        return Promise.resolve(arr)
      }).then((res) => {
        ctx.commit('initPlatformApp', res)
        return Promise.resolve('200')
      })
    }
  },
}